package com.mashibing.apipassenger.service.impl;

import com.mashibing.apipassenger.feign.ServicePassengerUserFeign;
import com.mashibing.apipassenger.service.UserService;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.dto.TokenResult;
import com.mashibing.internalcommon.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @title: UserServiceImpl
 * @Author Z先森
 * @Date: 2022/9/23 16:28
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private ServicePassengerUserFeign servicePassengerUserFeign;
    @Override
    public ResponseResult getUserByAccessToken(String accessToken) {
        //解析accessToken 获取手机号
        TokenResult tokenResult = JwtUtils.checkToken(accessToken);
        String passengerPhone = tokenResult.getPhone();
        //根据手机号查询用户信息
        ResponseResult user = servicePassengerUserFeign.getUser(passengerPhone);
        if (Objects.isNull(user.getData())){
            return ResponseResult.fail(user.getCode(),user.getMessage());
        }
        return ResponseResult.success(user.getData());
    }
}
